package com.znufe.exam.service.excel;

import com.alibaba.excel.util.StringUtils;
import com.znufe.exam.domain.Student;
import com.znufe.exam.domain.students.StudentValidationObject;
import com.znufe.exam.repository.StudentMapper;
import com.znufe.exam.utility.easyexcel.ExcelCheckErrDto;
import com.znufe.exam.utility.easyexcel.ExcelCheckResult;
import com.znufe.exam.viewmodel.admin.student.StudentFromExcelVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@Service
public class ExcelServiceImpl implements ExcelService {

    private final  StudentMapper studentMapper;

    @Autowired
    public ExcelServiceImpl(StudentMapper studentMapper) {
        this.studentMapper = studentMapper;
    }

    @Override
    public ExcelCheckResult checkImportExcel(List<StudentFromExcelVM> studentVMList, Integer id) {

        //成功结果集
        List<StudentFromExcelVM> successList = new ArrayList<>();
        //错误数组
        List<ExcelCheckErrDto<StudentFromExcelVM>> errList = new ArrayList<>();

        System.out.println("哈哈， 我在serviceImpl里的 checkImportExcel拿到班级id：" + id + " 了！");
        List<StudentValidationObject> validationInfoList = studentMapper.selectValidationByClassId(id);
        // 构造两个Set 用来存储数据库某个班级中已经存在的 答题器编号 与 学生电话
        for (StudentValidationObject validation : validationInfoList) {
            System.out.println(validation);
        }
        Set<Integer> existVoteIds = new HashSet<>();
        Set<String> existStuPhones = new HashSet<>();
        for (StudentValidationObject validation : validationInfoList) {
            existVoteIds.add(validation.getVoteId());
            existStuPhones.add(validation.getStuPhone());
        }

        Set<Integer> inputVoteIds = new HashSet<>();
        Set<String> inputStuPhones = new HashSet<>();
        for (StudentFromExcelVM studentVM : studentVMList) {
            // 错误信息
            StringBuilder errMsg = new StringBuilder();

            // 根据自己的业务去做判断
            // 判断答题器编号与学生电话是否重复
            String stuName = studentVM.getStuName();
            if (null == stuName) {
                errMsg.append("学生姓名不能为空，请输入姓名！").append(";");
            }

            String gender = studentVM.getGender();
            if (null == gender) {
                errMsg.append("性别不能为空，请输入性别！").append(";");
            } else {
                if (!"男".equals(gender) && !"女".equals(gender)) {
                    errMsg.append("性别输入错误，性别只能输入“男”或“女”，请从下拉框选择！").append(";");
                }
            }

            if (null == studentVM.getVoteId()) {
                errMsg.append("答题器编号不能为空，请输入答题器编号！").append(";");
            } else {
                Integer voteId = Integer.parseInt(studentVM.getVoteId());
                if (existVoteIds.contains(voteId)) {
                    errMsg.append("此条数据输入的答题器编号已绑定学生,请更换！").append(";");
                } else {
                    if (!inputVoteIds.contains(voteId)) {
                        inputVoteIds.add(voteId);
                    } else {
                        errMsg.append("此条数据输入的答题器编号在您提交的文件中有重复,请调整！").append(";");
                    }
                }
            }

            String stuPhone = studentVM.getStuPhone();
            if (null == stuPhone) {
                errMsg.append("联系电话不能为空，请输入联系电话！").append(";");
            } else {
                if (existStuPhones.contains(stuPhone)) {
                    errMsg.append("此条数据输入的手机号已绑定学生，请更换！").append(";");
                } else {
                    if (!inputStuPhones.contains(stuPhone)) {
                        inputStuPhones.add(stuPhone);
                    } else {
                        errMsg.append("此条数据输入的手机号在您提交的文件中有重复，请调整！").append(";");
                    }
                }
            }

            String voteBaseId = studentVM.getVoteBaseId();
            if (null == voteBaseId) {
                errMsg.append("基站编号不能为空，请输入基站编号！").append(";");
            }

            if (StringUtils.isEmpty(errMsg.toString())) {
                // 这里有两个选择，1、一个返回成功的对象信息，2、进行持久化操作
                System.out.println("serviceImpl的 checkImportExcel里的 ：" + studentVM);
                successList.add(studentVM);
            } else {
                // 添加错误信息
                errList.add(new ExcelCheckErrDto(studentVM, errMsg.toString()));
            }
        }
        return new ExcelCheckResult(successList,errList);
    }
}
